package com.sh.rbac.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sh.rbac.core.common.constant.CacheConstants;
import com.sh.rbac.core.common.enums.StatusEnum;
import com.sh.rbac.domain.converter.ParamConverter;
import com.sh.rbac.domain.dto.param.ParamQueryDTO;
import com.sh.rbac.domain.dto.param.ParamSaveDTO;
import com.sh.rbac.domain.dto.param.ParamUpdateDTO;
import com.sh.rbac.domain.entity.Param;
import com.sh.rbac.mapper.ParamMapper;
import com.sh.rbac.service.ParamService;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author wb
 * @since 2025-02-11
 */

@Service
public class ParamServiceImpl extends ServiceImpl<ParamMapper, Param> implements ParamService {
    @Override
    public Page<Param> getPage(Long current, Long limit, ParamQueryDTO paramQueryDTO) {
        // 创建分页
        Page<Param> page = new Page<>(current, limit);

        // 获取查询条件
        String paramName = paramQueryDTO.getParamName();
        String paramCode = paramQueryDTO.getParamCode();
        Integer paramType = paramQueryDTO.getParamType();

        // 构建条件
        LambdaQueryWrapper<Param> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(paramName), Param::getParamName, paramName)
                .like(StrUtil.isNotBlank(paramCode), Param::getParamCode, paramCode)
                .eq(paramType != null, Param::getParamType, paramType);

        // 返回查询结果
        return baseMapper.selectPage(page, lqw);
    }

    @Override
    public Boolean save(ParamSaveDTO paramSaveDTO) {
        Param param = ParamConverter.INSTANCE.toEntity(paramSaveDTO);
        return super.save(param);
    }


    @CacheEvict(value = CacheConstants.PARAM_KEY, allEntries = true)
    @Override
    public boolean removeById(Serializable id) {
        return super.removeById(id);
    }

    @CacheEvict(value = CacheConstants.PARAM_KEY, allEntries = true)
    @Override
    public boolean removeBatchByIds(Collection<?> list) {
        return super.removeBatchByIds(list);
    }

    @CacheEvict(value = CacheConstants.PARAM_KEY, key = "#paramUpdateDTO.paramCode")
    @Override
    public Boolean updateById(ParamUpdateDTO paramUpdateDTO) {
        Param param = ParamConverter.INSTANCE.toEntity(paramUpdateDTO);
        return super.updateById(param);
    }

    @Override
    public Boolean updateStatus(Long id, Integer status) {
        Param param = new Param();
        param.setId(id);
        param.setStatus(status);

        return super.updateById(param);
    }

    @Cacheable(value = CacheConstants.PARAM_KEY, key = "#code")
    @Override
    public String getParamValueByCode(String code) {
        LambdaQueryWrapper<Param> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Param::getParamCode, code);
        lqw.eq(Param::getStatus, StatusEnum.NORMAL);
        Param param = super.getOne(lqw);

        if(param == null){
            return null;
        }

        return param.getParamValue();
    }
}
